// pages/spell_place/spell_place.js
var app = getApp();

var common = require("../../../common/common.js").common;

var util = require("../../../utils/util.js");

Page({
    /**
   * 页面的初始数据
   */
    data: {
        only_express: 0,
        getLeaderNum: 0,
        choose_type: 1,
        //取货方式
        type: 0,
        //购买方式
        proceed_id: "",
        //团id
        buy_count: 0,
        //购买数量
        sku_id: "",
        //规格id
        pro: [],
        //商品
        all_price: 0,
        //合计
        other_money: 0,
        //其他费用
        myleaderinfo: "",
        //团长信息
        on_off_trans_free: 0,
        //满额包邮是否开启
        trans_for_free_barrier: 0,
        //满额包邮金额
        difference: 0,
        //还差多少包邮
        pay_price: 0,
        //付款金额
        status: 1,
        //可付款状态,
        buy_info: "",
        //购买个人信息
        confirm_group: 0,
        //确认团长弹窗
        sku_id_list: [],
        //购买商品规格和数量
        name: "",
        //姓名
        phone: "",
        //手机号
        house_number: "",
        //详细地址
        remark: "",
        //备注
        prepay_time: 1,
        leader_change: 2,
        balance_confirm: 0,
        payWayList: [ {
            iconfonts: "icon-yue",
            //支付方式icon
            payWayName: "余额",
            //支付方式名称
            isChecked: true,
            //radio状态
            color: "#FFAE00",
            //icon颜色,
            isDisabled: false
        }, {
            iconfonts: "icon-weixinzhifu",
            payWayName: "微信支付",
            isChecked: false,
            color: "#45C144",
            isDisabled: false
        } ],
        //支付方式
        balanceNum: 13,
        //余额金额
        balanceNumTip: "",
        isShowPay: false,
        //是否显示充值按钮
        tmplIds: [],
        subscribeMessage: true,
        lat: "",
        lng: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        var isIPX = app.globalData.isIPX;
        console.log("isIPX:" + isIPX);
        this.setData({
            isIPX: isIPX
        });
        let that = this;
        that.setData({
            type: options.type,
            proceed_id: options.group_id,
            sku_id: options.sku_id,
            buy_count: options.buy_count,
            balanceNumTip: "（余额：￥" + 0 + "）"
        });
        that.getBase();
        that.getBalance();
        that.getInfo();
        that.getExpressInfo();
        that.getTmplIds();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        let that = this;
        that.getBase();
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //纯快递费用
    getExpressInfo() {
        let that = this;
        common.request({
            endpoint: "Order/common/express-fee",
            method: "post",
            data: {},
            success(res) {
                if (res.extra) {
                    that.setData({
                        ...res.extra
                    });
                }
                that.getGoods();
            }
        });
    },
    getRecieverAddress() {
        var that = this;
        return new Promise((resolve, reject) => {
            common.getAppBase().then(() => {
                wx.request({
                    url: "http://apis.map.qq.com/ws/geocoder/v1/",
                    method: "get",
                    data: {
                        address: that.data.house_number,
                        key: that.data.appBase.location_key
                    },
                    success: function(res) {
                        if (res.data) {
                            that.setData({
                                lat: res.data.result.location.lat,
                                lng: res.data.result.location.lng
                            });
                        }
                        resolve();
                    }
                });
            });
        });
    },
    //选择取货方式
    chooseMethod: function(e) {
        let that = this;
        let choose_type = e.currentTarget.dataset.type;
        let cost = e.currentTarget.dataset.cost;
        let other_money = cost;
        let all_price = that.data.all_price;
        if (choose_type == 2) {
            //平台差额
            var difference = parseFloat(that.data.trans_for_free_barrier) - parseFloat(all_price);
        } else {
            var difference = parseFloat(that.data.pinkage_price) - parseFloat(all_price);
        }
        //平台配送或者团长配送 并且 开启了包邮，判断是否满额包邮
                if (choose_type == 2 && that.data.on_off_trans_free == 1 || choose_type == 0 && that.data.pinkage == 1) {
            if (difference <= 0) {
                other_money = 0;
                that.setData({
                    other_money: 0
                });
            }
        }
        that.setData({
            choose_type: choose_type,
            other_money: other_money,
            difference: parseFloat(difference).toFixed(2)
        });
        var sku_id = that.data.sku_id;
        var buy_count = that.data.buy_count;
        //调整配送方式码
                var choose_type2;
        if (that.data.only_express == 1) {
            choose_type2 = 4;
        } else {
            if (choose_type == 1) {
                choose_type2 = 1;
            } else if (choose_type == 0) {
                choose_type2 = 2;
            } else if (choose_type == 2) {
                choose_type2 = 3;
            }
        }
        let sku_id_list = [ {
            sku_id: sku_id,
            num: buy_count,
            delivery_channel: choose_type2
        } ];
        var pay_price = parseFloat(all_price) + parseFloat(other_money);
        that.setData({
            pay_price: parseFloat(pay_price).toFixed(2),
            sku_id_list: sku_id_list
        });
        //判断余额
                that.balanceIsOpen(parseFloat(pay_price).toFixed(2));
    },
    //获取团长
    getGroup: function() {
        let that = this;
        var getLeaderNum = that.data.getLeaderNum + 1;
        that.setData({
            getLeaderNum: getLeaderNum
        });
        if (getLeaderNum == 1) {} else {
            let leaderName = that.data.leaderName;
            common.request({
                endpoint: "leader/myleaderinfo",
                method: "post",
                data: {
                    leader_id_share: "",
                    openid: wx.getStorageSync("user").openid
                },
                success: res => {
                    if (!res.extra.leader_id) {
                        wx.showModal({
                            title: "提示",
                            content: "您还没有选择" + leaderName + "，请先选择" + leaderName + "后再下单！",
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                    wx.redirectTo({
                                        url: "/pages/nearby_group2/nearby_group2"
                                    });
                                }
                            }
                        });
                    }
                    var typeBox = res.extra.trans_type;
                    typeBox.map(item => {
                        if (item.type == 0) {
                            that.setData({
                                pinkage: item.pinkage,
                                //团长配满减配送关闭开启
                                pinkage_price: item.pinkage_price
                            });
                        }
                    });
                    that.setData({
                        myleaderinfo: res.extra,
                        //团长信息
                        choose_type: res.extra.trans_type[0].type,
                        //默认配送方式
                        other_money: res.extra.trans_type[0].cost,
                        //配送费
                        on_off_trans_free: res.extra.on_off_trans_free,
                        //包邮开关
                        trans_for_free_barrier: res.extra.trans_for_free_barrier
                    });
                    //请求产品数据，价格，生成最终价格
                                        that.getGoods();
                }
            });
        }
    },
    //获取购买商品
    getGoods: function() {
        let that = this;
        let group_id = that.data.proceed_id;
        let type = that.data.type;
        let buy_count = that.data.buy_count;
        let sku_id = that.data.sku_id;
        var choose_type = that.data.choose_type;
        //调整配送方式码
                var choose_type2;
        if (that.data.only_express == 1) {
            choose_type2 = 4;
        } else {
            if (choose_type == 1) {
                choose_type2 = 1;
            } else if (choose_type == 0) {
                choose_type2 = 2;
            } else if (choose_type == 2) {
                choose_type2 = 3;
            }
        }
        console.log("配送方式");
        console.log(choose_type);
        console.log(choose_type2);
        let sku_id_list = [ {
            sku_id: sku_id,
            num: buy_count,
            delivery_channel: choose_type2
        } ];
        common.request({
            endpoint: "Groupbuy/product/important",
            method: "post",
            data: {
                type: type,
                type_key: group_id,
                sku_id_list: sku_id_list
            },
            header: {},
            success: function(res) {
                let pro = [];
                if (res.extra.default.length) {
                    pro = res.extra.default;
                } else if (res.extra.express.length) {
                    pro = res.extra.express;
                    that.setData({
                        only_express: 1
                    });
                }
                let all_price = 0;
                //商品总价
                                for (var i = 0; i < pro.length; i++) {
                    all_price += pro[i].price * buy_count;
                    if (pro[i].stock_number < buy_count) {
                        wx.showModal({
                            title: "提示",
                            content: "库存不足",
                            success: function() {
                                if (res.confirm) {
                                    wx.navigateBack();
                                }
                            }
                        });
                    }
                }
                let other_money = that.data.other_money;
                //配送费用
                                var difference;
                if (that.data.only_express == 1) {
                    difference = parseFloat(that.data.express_fms_fee) - parseFloat(all_price);
                    if (that.data.express_fms == 1 && difference <= 0) {
                        that.setData({
                            other_money: 0
                        });
                    } else {
                        other_money = that.data.express_fee;
                        that.setData({
                            other_money: other_money
                        });
                    }
                } else {
                    if (that.data.choose_type == 2) {
                        //平台差额
                        difference = parseFloat(that.data.trans_for_free_barrier) - parseFloat(all_price);
                    } else {
                        difference = parseFloat(that.data.pinkage_price) - parseFloat(all_price);
                    }
                    //平台配送或者团长配送 并且 开启了包邮，判断是否满额包邮
                                        if (that.data.choose_type == 2 && that.data.on_off_trans_free == 1 || that.data.choose_type == 0 && that.data.pinkage == 1) {
                        if (difference <= 0) {
                            other_money = 0;
                            that.setData({
                                other_money: 0
                            });
                        }
                    }
                }
                console.log("other_money" + other_money);
                let pay_price = parseFloat(all_price) + parseFloat(other_money);
                //实付金额
                                that.setData({
                    pro: pro,
                    pay_price: parseFloat(pay_price).toFixed(2),
                    difference: parseFloat(difference).toFixed(2),
                    all_price: parseFloat(all_price).toFixed(2),
                    sku_id_list: sku_id_list
                });
                //判断余额
                                that.balanceIsOpen(parseFloat(pay_price).toFixed(2));
            }
        });
    },
    //获取团长信息
    getGroup2: function() {
        let that = this;
        let leaderName = that.data.leaderName;
        if (getLeaderNum == 1) {} else {
            common.request({
                endpoint: "leader/myleaderinfo",
                method: "post",
                data: {
                    leader_id_share: "",
                    openid: wx.getStorageSync("user").openid
                },
                success: res => {
                    if (res.extra.length == 0) {
                        wx.showModal({
                            title: "提示",
                            content: "您还没有选择" + leaderName + "，请先选择" + leaderName + "后再下单！",
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                    wx.redirectTo({
                                        url: "/pages/nearby_group2/nearby_group2"
                                    });
                                }
                            }
                        });
                    }
                }
            });
        }
    },
    // //确认团长
    // confirm_group: function(e) {
    //   let that = this;
    //   var name = e.detail.value.name;
    //   var phone = e.detail.value.phone;
    //   var house_number = e.detail.value.house_number;
    //   var remark = e.detail.value.remark;
    //   let form_id = e.detail.formId;
    //   if (!name) {
    //     wx.showModal({
    //       title: '提醒',
    //       content: '姓名为必填！',
    //       showCancel: false
    //     })
    //     return false;
    //   }
    //   if (!phone) {
    //     wx.showModal({
    //       title: '提醒',
    //       content: '手机号为必填！',
    //       showCancel: false
    //     })
    //     return false;
    //   }
    //   var reg2 = /^[1][0-9][0-9]{9}$/;
    //   if (!reg2.test(e.detail.value.phone)) {
    //     wx.showModal({
    //       title: '提示',
    //       content: '请输入正确的手机号码！',
    //       showCancel: false
    //     })
    //     return false;
    //   }
    //   let choose_type = that.data.choose_type
    //   if (choose_type != 1) {
    //     if (!house_number) {
    //       wx.showModal({
    //         title: '提醒',
    //         content: '地址为必填！',
    //         showCancel: false
    //       })
    //       return false;
    //     }
    //   }
    //   that.setData({
    //     status: 0,
    //     buy_info: e.detail.value,
    //     form_id: form_id,
    //     confirm_group: 1
    //   })
    // },
    confirm_group: function() {
        let that = this;
        that.confirm_group_ch();
        if (that.data.subscribeMessage) {
            wx.requestSubscribeMessage({
                tmplIds: that.data.tmplIds,
                success(res) {},
                fail(err) {
                    console.log(err);
                }
            });
        }
    },
    confirm_group_ch: function() {
        let that = this;
        var name = that.data.name;
        var phone = that.data.phone;
        var house_number = that.data.house_number;
        var remark = that.data.remark;
        if (!name) {
            wx.showModal({
                title: "提醒",
                content: "姓名为必填！",
                showCancel: false
            });
            return false;
        }
        if (!phone) {
            wx.showModal({
                title: "提醒",
                content: "手机号为必填！",
                showCancel: false
            });
            return false;
        }
        var reg2 = /^[1][0-9][0-9]{9}$/;
        if (!reg2.test(phone)) {
            wx.showModal({
                title: "提示",
                content: "请输入正确的手机号码！",
                showCancel: false
            });
            return false;
        }
        let choose_type = that.data.choose_type;
        if (choose_type != 1) {
            if (!house_number) {
                wx.showModal({
                    title: "提醒",
                    content: "地址为必填！",
                    showCancel: false
                });
                return false;
            }
        }
        let buy_info = {
            name: name,
            phone: phone,
            house_number: house_number,
            remark: remark
        };
        that.setData({
            status: 0,
            buy_info: buy_info
        });
        if (that.data.only_express == 1) {
            that.prepayorder();
        } else {
            if (that.data.choose_type == 2) {
                that.prepayorder();
            } else {
                that.setData({
                    confirm_group: 1
                });
            }
        }
    },
    //保存购买信息
    recordValue: function(e) {
        let key = e.currentTarget.dataset.name;
        let value = e.detail.value;
        this.setData({
            [key]: value
        });
        if (key == "house_number") {
            this.getRecieverAddress();
        }
    },
    remind: function() {
        wx.showModal({
            title: "提示",
            content: "请勿重复点击支付按钮！"
        });
    },
    //关闭确认团长
    close_confirm: function() {
        this.setData({
            confirm_group: 0,
            status: 1
        });
    },
    //下单
    prepayorder: function() {
        let that = this;
        let buy_info = that.data.buy_info;
        let leader_id = that.data.myleaderinfo.leader_id;
        let sku_id_list = that.data.sku_id_list;
        let form_id = that.data.form_id;
        let proceed_id = that.data.proceed_id;
        let type = that.data.type;
        var choose_type = that.data.choose_type;
        var delivery_channel = "";
        if (that.data.only_express == 1) {
            delivery_channel = "";
        } else {
            if (choose_type == 0) {
                delivery_channel = "leader";
            } else if (choose_type == 1) {
                delivery_channel = "self";
            } else {
                delivery_channel = "seller";
            }
        }
        that.setData({
            prepay_time: 0
        });
        var payWayList = that.data.payWayList;
        var balance_status = 0;
        payWayList.forEach((item, index) => {
            if (item.isChecked) {
                if (index == 0) {
                    balance_status = 1;
                }
            }
        });
        common.request({
            endpoint: "Groupbuy/common/prepayorder",
            method: "post",
            data: {
                name: buy_info.name,
                phone: buy_info.phone,
                house_number: buy_info.house_number,
                remark: buy_info.remark,
                leader_id: leader_id,
                items: sku_id_list,
                form_id: form_id,
                delivery_channel: delivery_channel,
                operation: 3,
                proceed_id: proceed_id,
                type: type,
                balance_status: balance_status,
                address_name: "",
                lat: that.data.lat,
                lng: that.data.lng
            },
            success: function(res) {
                let order_no = res.order_no;
                that.setData({
                    confirm_group: 0,
                    order_no: order_no
                });
                //支付方式选择
                                that.payWay(res, order_no);
                // wx.requestPayment({
                //   'timeStamp': res.pay.timeStamp,
                //   'nonceStr': res.pay.nonceStr,
                //   'package': res.pay.package,
                //   'signType': res.pay.signType,
                //   'paySign': res.pay.paySign,
                //   'success':function(res){
                //     wx.showToast({
                //       title: '购买成功！',
                //       icon: 'success',
                //       success: function () {
                //         common.request({
                //           endpoint: 'Groupbuy/common/syncnotify',
                //           method: 'post',
                //           data: {
                //             order_no: order_no, 
                //           },
                //           success: res => {
                //             if(type!=0){
                //               setTimeout(function () {
                //                 wx.reLaunch({
                //                   url: '/pages/spell_success/spell_success?order_num=' + order_no + '&type=' + type,
                //                 })
                //               }, 500); 
                //             }else{
                //               setTimeout(function () {
                //                 wx.reLaunch({
                //                   url: '/pages/order_confirm/order_confirm?order_no=' + order_no,
                //                 })
                //               }, 500); 
                //             }
                //           },  
                //         })
                //       },
                //     })
                //   },
                //   fail: res => {
                //     if(type==0){
                //       wx.redirectTo({
                //         url: '/pages/order/order',
                //       })
                //     }else{
                //       wx.redirectTo({
                //         url: '/pages/my_spell/my_spell',
                //       })
                //     }
                //   }
                // })
                        },
            fail: function(res) {
                wx.showModal({
                    title: "提示",
                    content: res.message
                });
            }
        });
    },
    //获取个人信息
    getInfo: function() {
        let openid = wx.getStorageSync("user").openid;
        let that = this;
        common.request({
            endpoint: "member/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success(res) {
                console.log(res);
                that.setData({
                    name: res.extra.name,
                    phone: res.extra.phone,
                    house_number: res.extra.address_other,
                    remark: res.extra.remark
                });
                that.getRecieverAddress();
            }
        });
    },
    leader_change: function(e) {
        var address = e.currentTarget.dataset.address;
        var is_leader = wx.getStorageSync("my_leader").is_leader;
        let leaderName = this.data.leaderName;
        if (is_leader == 1) {
            wx.showModal({
                title: "提示",
                content: "您自己是" + leaderName + "不能切换" + leaderName + "！"
            });
        }
        if (this.data.leader_change == 1 && is_leader != 1) {
            wx.navigateTo({
                url: "/pages/nearby_group2/nearby_group2"
            });
        }
    },
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    leader_change: res.extra.l_change,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    communityName: res.extra.community_title ? res.extra.community_title : "社区",
                    color_main: res.extra.color_main
                });
                that.getGroup();
                //我的团长信息
                        }
        });
    },
    //余额功能是否开通
    balanceIsOpen(totalPrice) {
        var that = this;
        //that.isCanUseBalance(totalPrice, 0)
                console.log("判断");
        common.request({
            endpoint: "Balance/balance/balance_switch",
            method: "post",
            data: {},
            success(res) {
                that.setData({
                    balanceSwitch: res.extra
                });
                if (res.extra == 1) {
                    //余额开通，判断余额是否足够支付
                    that.isCanUseBalance(totalPrice, 1);
                } else {
                    //未开通
                    that.isCanUseBalance(totalPrice, 0);
                }
            }
        });
    },
    //判断余额够不够支付
    isCanUseBalance(totalPrice, balanceIsOpen) {
        //balanceIsOpen,余额开通1，余额未开通0
        //totalPrice实付金额
        var that = this;
        //获取支付方式状态
                var payWayList = that.data.payWayList;
        //获取余额
                var balanceNum = that.data.balanceNum;
        console.log(totalPrice + "-" + balanceNum);
        if (parseFloat(totalPrice) <= parseFloat(balanceNum) && balanceIsOpen == 1) {
            console.log("已开通");
            //余额开通并且足够支付，默认选余额支付
                        that.setData({
                payWayList: payWayList
            });
        } else {
            console.log("未开通");
            //余额不足
            //余额项提示余额不足
            //显示跳转充值按钮
            //获取支付方式列表
                        console.log("ewqewqe");
            // var payWayList = that.data.payWayList;
                        payWayList.forEach((item, index) => {
                if (index == 0) {
                    //余额选项禁用，
                    item.isDisabled = true;
                    item.isChecked = false;
                } else if (index == 1) {
                    //默认选择第二个，
                    item.isChecked = true;
                } else {
                    item.isChecked = false;
                }
            });
            console.log(payWayList);
            that.setData({
                payWayList: payWayList,
                //更新支付列表
                balanceNumTip: "（余额不足：￥" + that.data.balanceNum + "）",
                //列表项余额不足提示
                isShowPay: false
            });
        }
    },
    //获取余额
    getBalance() {
        var that = this;
        common.request({
            endpoint: "Balance/balance/mybalance",
            method: "post",
            data: {},
            success(res) {
                console.log(res, "balance");
                that.setData({
                    balanceNum: res.extra.balance,
                    balanceNumTip: "（余额：￥" + res.extra.balance + "）"
                });
            },
            fail(err) {
                console.log(err);
            }
        });
    },
    radioChange(e) {
        console.log(e);
        var data = this.data.payWayList;
        data.forEach((item, index) => {
            if (index == e.detail.value) {
                item.isChecked = true;
            } else {
                item.isChecked = false;
            }
        });
        this.setData({
            //radioCurrent: e.detail.value
            payWayList: data
        });
    },
    /**
   * 
   * 支付方式
   * 
   */
    payWay(res, order_no) {
        let that = this;
        let type = that.data.type;
        //下单成功后，判断支付方式
        //获取支付方式列表
                var payWayList = that.data.payWayList;
        payWayList.forEach((item, index) => {
            if (item.isChecked) {
                //找到选中项
                switch (index) {
                  case 0:
                    //余额支付
                    console.log("余额支付");
                    //余额支付弹窗确认
                                        that.setData({
                        balance_confirm: 1
                    });
                    break;

                  case 1:
                    //微信支付
                    wx.requestPayment({
                        timeStamp: res.pay.timeStamp,
                        nonceStr: res.pay.nonceStr,
                        package: res.pay.package,
                        signType: res.pay.signType,
                        paySign: res.pay.paySign,
                        success: function(res) {
                            wx.showToast({
                                title: "购买成功！",
                                icon: "success",
                                success: function() {
                                    common.request({
                                        endpoint: "Order/common/sync-notify",
                                        method: "post",
                                        data: {
                                            order_no: order_no
                                        },
                                        success: res => {
                                            if (type != 0) {
                                                setTimeout(function() {
                                                    wx.reLaunch({
                                                        url: "/spell/pages/spell_success/spell_success?order_num=" + order_no + "&type=" + type
                                                    });
                                                }, 500);
                                            } else {
                                                setTimeout(function() {
                                                    wx.reLaunch({
                                                        url: "/pages/order_confirm/order_confirm?order_no=" + order_no
                                                    });
                                                }, 500);
                                            }
                                        },
                                        fail(err) {
                                            wx.showModal({
                                                title: "提示",
                                                content: err.message
                                            });
                                        }
                                    });
                                }
                            });
                        },
                        fail: res => {
                            if (type == 0) {
                                wx.switchTab({
                                    url: "/pages/order/order"
                                });
                            } else {
                                wx.redirectTo({
                                    url: "/pages/my_spell/my_spell"
                                });
                            }
                        }
                    });
                    break;
                }
            }
        });
    },
    confirm_balance() {
        var that = this;
        var order_no = that.data.order_no;
        let type = that.data.type;
        common.request({
            endpoint: "Order/common/sync-notify",
            method: "post",
            data: {
                order_no: order_no
            },
            success: res => {
                if (type != 0) {
                    setTimeout(function() {
                        wx.reLaunch({
                            url: "/spell/pages/spell_success/spell_success?order_num=" + order_no + "&type=" + type
                        });
                    }, 500);
                } else {
                    setTimeout(function() {
                        wx.reLaunch({
                            url: "/pages/order_confirm/order_confirm?order_no=" + order_no
                        });
                    }, 500);
                }
            },
            fail(err) {
                wx.showModal({
                    title: "提示",
                    content: err.message
                });
            }
        });
    },
    close_balance_confirm() {
        this.setData({
            balance_confirm: 0
        });
        wx.navigateBack({});
    },
    //获取tmplIds
    getTmplIds: function() {
        let that = this;
        const version = wx.getSystemInfoSync().SDKVersion;
        //console.log(version);
                if (util.compareVersion(version, "2.8.2") < 0) {
            that.setData({
                tmplIds: [],
                subscribeMessage: false
            });
            return;
        }
        common.request({
            endpoint: "set/template_get",
            method: "post",
            data: {},
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                let tmplIds = [];
                let subscribeMessage = true;
                res.extra.map(v => {
                    if ((v.type == "24" || v.type == "25" || v.type == "29") && v.status == "1") {
                        tmplIds.push(v.template_id);
                    }
                });
                if (!tmplIds.length) {
                    let subscribeMessage = false;
                }
                that.setData({
                    tmplIds: tmplIds,
                    subscribeMessage: subscribeMessage
                });
            }
        });
    }
});